﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using HotelManagement.Class;

namespace HotelManagement
{
    public partial class RetrieveStaffMenu1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                // Display Staff Name into list box 1
                displayStaffName();

                //Display Staff ID into list box 2
                displayStaffID();

            }
        }
        

        protected void btnRetrieveStaffByName_Click(object sender, EventArgs e)
        {
            try
            {
                Server.Transfer("RetrieveStaff.aspx?StaffID=" + listBoxRetrieveByName.SelectedValue);
            }
            catch (Exception)
            {
                lblStatus.Text = "Please Select Staff Name to Search";
            
            }
      
        }

        protected void btnRetrieveStaffByID_Click(object sender, EventArgs e)
        {
           try
            {                       
                Server.Transfer("RetrieveStaff.aspx?StaffID=" + listBoxRetrieveByID.SelectedValue);
            }
            catch (Exception)
            {
                lblStatus.Text = "Please Select Staff ID to Search";
            }
       }

        protected void btnRetrieveStaffByDutyTypes_Click(object sender, EventArgs e)
        {
            try
            {
                displayGridView();
            }
            catch (Exception ex)
            {
                lblStatus.Text = "Error: " + ex.Message;
            }
        }

        protected void btnGoBackToStaffMenu_Click(object sender, EventArgs e)
        {
            Server.Transfer("ManageHousekeepingAndStaff.aspx");
        }

        private void displayStaffName()
        {
            listBoxRetrieveByName.Items.Clear();
            string selectSQL = "SELECT FirstName,LastName, StaffID FROM Staff";

            SqlDataReader reader;

            try
            {
                SqlConnection con = StaffDBManager.ConnectToDatabase();

                SqlCommand cmd = new SqlCommand(selectSQL, con);
                ListItem newItem = new ListItem();


                con.Open();
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    newItem = new ListItem();
                    newItem.Text = reader["FirstName"].ToString() + " " + reader["LastName"].ToString();
                    newItem.Value = reader["StaffID"].ToString();
                    listBoxRetrieveByName.Items.Add(newItem);
                }
                reader.Close();


            }
            catch (SqlException Ex1)
            {
                lblStatus.Text = "Error: " + Ex1 + "Please try again";
            }

        }

        private void displayStaffID()
        {

            try
            {

                SqlConnection conn = StaffDBManager.ConnectToDatabase();
                DataTable ObjectData = new DataTable();
                string query = "SELECT [StaffID] from [Staff] ORDER BY StaffID ASC";
                SqlDataAdapter objda = new SqlDataAdapter(query, conn);
                conn.Open();
                objda.Fill(ObjectData);
                if (ObjectData.Rows.Count > 0)
                {
                    listBoxRetrieveByID.DataSource = ObjectData;
                    listBoxRetrieveByID.DataValueField = "StaffID";
                    listBoxRetrieveByID.DataBind();
                }

                conn.Close();
            }
            catch (SqlException Ex1)
            {
                lblStatus.Text = "Error: " + Ex1 + "Please try again";
            }
        }

        private void displayGridView()
        {
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter();
            if (ddlDutyType.SelectedIndex != 0)
            {
                SqlConnection con = StaffDBManager.ConnectToDatabase();
                con.Open();

                da = new SqlDataAdapter("SELECT DutyType, StaffID, FirstName, LastName, Title FROM Staff WHERE DutyType='" + ddlDutyType.SelectedValue + "'", con);
                da.Fill(ds);
                GridView1.DataSource = ds.Tables[0];
                GridView1.DataBind();
                con.Close();
            }
            else if (ddlDutyType.SelectedValue == "All")
            {
                SqlConnection con = StaffDBManager.ConnectToDatabase();
                con.Open();
                da = new SqlDataAdapter("SELECT DutyType, StaffID, FirstName, LastName, Title FROM Staff WHERE DutyType=DutyType", con);
                da.Fill(ds);
                GridView1.DataSource = ds.Tables[0];
                GridView1.DataBind();
                con.Close();
            }
        }


       
        
        }
    }
